﻿#include "widget.h"
#include <QDebug>

HWND helperWindowHandle;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_PaintOnScreen);

//    glEnable(GL_DEPTH_TEST);
//    glDepthFunc(GL_LESS);
//    glFrontFace(GL_CW);
//    glCullFace(GL_FRONT);
//    glEnable(GL_CULL_FACE);
    m_wnd = (HWND)winId();
    m_dc = GetDC(m_wnd);

    QWidget* twidget = new QWidget();
    twidget->hide();
    HDC tempHdc = GetDC((HWND)twidget->winId());
    if(!initWGL(m_dc,tempHdc))
    {
        qDebug()<<"gl init fail";
    }
    else
    {
        if(glewInit() != GLEW_OK)
            qDebug()<<"glew fail";
    }
    delete twidget;
    glPointSize(2.0f);
    glClearColor(0.8f, 0.7f, 0.5f, 0.0f);

    m_point[0] = -1.0f;
    m_point[1] = -1.0f;
    m_point[2] = 0.0f;
    m_point[3] = 1.0f;
    m_point[4] = -1.0f;
    m_point[5] = 0.0f;
    m_point[6] = 0.0f;
    m_point[7] = 1.0f;
    m_point[8] = 0.0f;
    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(m_point),m_point,GL_STATIC_DRAW);
}

Widget::~Widget()
{

}

void Widget::paintEvent(QPaintEvent *event)
{
    glClear(GL_COLOR_BUFFER_BIT);
    // 开启顶点属性
    glEnableVertexAttribArray(0);
    // 绑定GL_ARRAY_BUFFER缓冲器
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    // 告诉管线怎样解析bufer中的数据
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    // 开始绘制几何图形(绘制一个点)
    glDrawArrays(GL_TRIANGLES, 0, 3);

    //  禁用顶点数据
    glDisableVertexAttribArray(0);

    SwapBuffers(m_dc);
}
/*
bool Widget::initGL()
{
    m_wnd = (HWND)winId();
    m_dc = GetDC(m_wnd);

    QWidget* twidget = new QWidget();
    twidget->hide();
    helperWindowHandle = (HWND)twidget->winId();
    HDC tempHdc = GetDC(helperWindowHandle);

    int index = 0;
    PIXELFORMATDESCRIPTOR pfd;
    index = ChoosePixelFormat(tempHdc, &pfd);
    if(index == 0)
        return false;
    if(SetPixelFormat(tempHdc,index,&pfd) == FALSE)
        return false;

    HGLRC glrc = wglCreateContext(tempHdc);
    wglMakeCurrent(tempHdc,glrc);

    if(glewInit() != GLEW_OK)
        return false;

    wglMakeCurrent(0,0);
    wglDeleteContext(glrc);

    float fPixAttribs[]={0,0};
    int pixFormat = -1;
    int pixCount = -1;
    PIXELFORMATDESCRIPTOR pfd1;
    int iPixAttribs[] = {
    WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering
    WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window
    WGL_ACCELERATION_ARB,   WGL_FULL_ACCELERATION_ARB, // must be HW accelerated
    WGL_COLOR_BITS_ARB,     24, // 8 bits of each R, G and B
    WGL_DEPTH_BITS_ARB,     24, // 24 bits of depth precision for window, 典型情况下深度缓冲区都是24位的,试了几台机器都不支持32位深度缓冲区
    WGL_DOUBLE_BUFFER_ARB,GL_TRUE, // Double buffered context
    WGL_PIXEL_TYPE_ARB,      WGL_TYPE_RGBA_ARB, // pf should be RGBA type
    WGL_STENCIL_BITS_ARB, 8,//开启模板缓冲区,模板缓冲区位数=8
    WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, // MSAA on,开启多重采样
    WGL_SAMPLES_ARB,        4, // 4x MSAA ,多重采样样本数量为4
    0}; // NULL termination
    int attributeListInt[19];
    // Support for OpenGL rendering.
     attributeListInt[0] = WGL_SUPPORT_OPENGL_ARB;
     attributeListInt[1] = TRUE;

     // Support for rendering to a window.
     attributeListInt[2] = WGL_DRAW_TO_WINDOW_ARB;
     attributeListInt[3] = TRUE;

     // Support for hardware acceleration.
     attributeListInt[4] = WGL_ACCELERATION_ARB;
     attributeListInt[5] = WGL_FULL_ACCELERATION_ARB;

     // Support for 24bit color.
     attributeListInt[6] = WGL_COLOR_BITS_ARB;
     attributeListInt[7] = 24;

     // Support for 24 bit depth buffer.
     attributeListInt[8] = WGL_DEPTH_BITS_ARB;
     attributeListInt[9] = 24;

     // Support for double buffer.
     attributeListInt[10] = WGL_DOUBLE_BUFFER_ARB;
     attributeListInt[11] = TRUE;

     // Support for swapping front and back buffer.
     attributeListInt[12] = WGL_SWAP_METHOD_ARB;
     attributeListInt[13] = WGL_SWAP_EXCHANGE_ARB;

     // Support for the RGBA pixel type.
     attributeListInt[14] = WGL_PIXEL_TYPE_ARB;
     attributeListInt[15] = WGL_TYPE_RGBA_ARB;

     // Support for a 8 bit stencil buffer.
     attributeListInt[16] = WGL_STENCIL_BITS_ARB;
     attributeListInt[17] = 8;

     // Null terminate the attribute list.
     attributeListInt[18] = 0;

     const int iPixelFormatAttributeList[] =
     {
     WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,                        // 绘制到窗口
     WGL_SUPPORT_OPENGL_ARB,GL_TRUE,                            // 支持OpenGL
     WGL_ACCELERATION_ARB ,WGL_FULL_ACCELERATION_ARB ,        // 硬件加速
     WGL_DOUBLE_BUFFER_ARB,GL_TRUE,                            // 双缓冲
     WGL_PIXEL_TYPE_ARB,WGL_TYPE_RGBA_ARB,                    // RGBA
     WGL_COLOR_BITS_ARB,32,                                    // 颜色位数32
     WGL_DEPTH_BITS_ARB,24,                                    // 深度位数24
     WGL_STENCIL_BITS_ARB,8,                                    // 模板位数8
     WGL_SWAP_METHOD_ARB, WGL_SWAP_EXCHANGE_ARB,                // 双缓冲swap方式直接交换
     WGL_SAMPLES_ARB, 4,                                // 4倍抗锯齿
     0
     };
    const int iContextAttributeList[] =
    {
    WGL_CONTEXT_MAJOR_VERSION_ARB,4,                        // 主版本号
    WGL_CONTEXT_MINOR_VERSION_ARB,0,                        // 次版本号
    WGL_CONTEXT_FLAGS_ARB,WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
    0
    };
//    if(wglewInit() != GLEW_OK)
//        return false;
    if(!wglewGetExtension("WGL_ARB_pixel_format"))
        return false;
    if(!wglewGetExtension("WGL_ARB_create_context"))
    {
        return false;
    }
    wglChoosePixelFormatARB(m_dc,attributeListInt,0,1,&pixFormat,(uint*)&pixCount);

    if(pixFormat == -1)
    {
        iPixAttribs[18] = 1;
        wglChoosePixelFormatARB(m_dc,iPixAttribs,NULL,1,&pixFormat,(uint*)&pixCount);
    }

    if(!SetPixelFormat(m_dc,5,&pfd1))
        return false;
    glrc = wglCreateContextAttribsARB(m_dc,0,iContextAttributeList);
    if(glrc)
    {
        wglMakeCurrent(m_dc,glrc);
        return true;
    }
    return false;
}
*/
